package com.taxi.driver.module.main.mine.wallet;

import android.content.Context;
import android.content.Intent;
import android.os.Bundle;
import android.support.v4.widget.SwipeRefreshLayout;
import android.view.View;
import android.widget.Button;
import android.widget.RelativeLayout;
import android.widget.TextView;

import com.taxi.driver.BuildConfig;
import com.taxi.driver.R;
import com.taxi.driver.common.Application;
import com.taxi.driver.common.BaseActivity;
import com.taxi.driver.event.MsgEvent;
import com.taxi.driver.module.main.mine.wallet.dagger.DaggerMyWalletComponent;
import com.taxi.driver.module.main.mine.wallet.dagger.MyWalletModule;
import com.taxi.driver.module.vo.MyWalletVO;
import com.taxi.driver.util.Navigate;

import org.greenrobot.eventbus.EventBus;

import javax.inject.Inject;

import butterknife.BindView;
import butterknife.ButterKnife;
import butterknife.OnClick;

/**
 * 我的钱包
 */
public class MyWalletActivity extends BaseActivity implements MyWalletContract.View, SwipeRefreshLayout.OnRefreshListener {

    public static final String MESSAGE_UUID = "MESSAGE_UUID";
    public static final String MESSAGE_STATUS = "MESSAGE_STATUS";

    public static void start(Context context, String msgUuid, int msgStatus) {
        Intent starter = new Intent(context, MyWalletActivity.class);
        starter.putExtra(MESSAGE_UUID, msgUuid);
        starter.putExtra(MESSAGE_STATUS, msgStatus);
        context.startActivity(starter);
    }

    @BindView(R.id.swipe_refresh_layout)
    SwipeRefreshLayout mSwipeRefreshLayout;
    @BindView(R.id.tv_balance)
    TextView mTvBalance;
    @BindView(R.id.tv_poundage_title)
    TextView mTvPoundageTitle;
    @BindView(R.id.btn_withdrawal)
    Button mBtnWithdrawal;
    @BindView(R.id.rl_bill)
    RelativeLayout mRlBill;
    @BindView(R.id.rl_withdrawal_record)
    RelativeLayout mRlWithdrawalRecord;
    @BindView(R.id.rl_specification)
    RelativeLayout mRlSpecification;

    @Inject
    MyWalletPresenter mPresenter;

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_my_wallet);
        ButterKnife.bind(this);

        DaggerMyWalletComponent.builder()
                .appComponent(Application.getAppComponent())
                .myWalletModule(new MyWalletModule(this))
                .build().inject(this);
        mSwipeRefreshLayout.setOnRefreshListener(this);

        String msgUuid = getIntent().getStringExtra(MESSAGE_UUID);
        int msgStatus = getIntent().getIntExtra(MESSAGE_STATUS, 0);
        if (msgUuid != null) {
            mPresenter.updateMsg(msgUuid, msgStatus);
        }
        //隐藏提现
        if (BuildConfig.FLAVOR_project.equals("Q201705XuanXuan")) {
            mBtnWithdrawal.setVisibility(View.GONE);
            mBtnWithdrawal.setEnabled(false);
            mTvPoundageTitle.setVisibility(View.GONE);
            mTvPoundageTitle.setEnabled(false);
            mRlWithdrawalRecord.setVisibility(View.GONE);
            mRlWithdrawalRecord.setEnabled(false);
        }
    }

    @Override
    protected void onResume() {
        super.onResume();
        mPresenter.reqDriverInfo();
    }

    @OnClick({R.id.rl_bill, R.id.rl_withdrawal_record, R.id.rl_specification,
            R.id.btn_withdrawal, R.id.img_head_left})
    public void onClick(View view) {
        switch (view.getId()) {
            case R.id.rl_bill:
                Navigate.openBill(MyWalletActivity.this);
                break;
            case R.id.rl_withdrawal_record:
                Navigate.openWithdrawalRecord(MyWalletActivity.this);
                break;
            case R.id.rl_specification:
                Navigate.openRules(MyWalletActivity.this);
                break;
            case R.id.btn_withdrawal:
                Navigate.openWithdrawal(MyWalletActivity.this);
                break;
            case R.id.img_head_left:
                finish();
                break;
        }
    }

    @Override
    public int getBarColor() {
        return R.color.theme_color;
    }

    @Override
    public boolean isActive() {
        return false;
    }

    @Override
    public void onRefresh() {
        mPresenter.reqDriverInfo();
    }

    @Override
    public void showDriverInfo(MyWalletVO vo) {
        mSwipeRefreshLayout.setRefreshing(false);
        mTvBalance.setText(String.format("%.01f", vo.balance));
        mTvPoundageTitle.setText(vo.poundageTitle);
        if (vo.canWishdraw == 1) {
            mBtnWithdrawal.setEnabled(true);
            mBtnWithdrawal.setBackgroundResource(R.drawable.sel_dark_btn);
        } else {
            mBtnWithdrawal.setEnabled(false);
            mBtnWithdrawal.setBackgroundResource(R.drawable.dra_dark_btn_cant_press);
        }
    }

    @Override
    public void updateSuccess(String uuid) {
        // 发送事件通知消息列表改变状态
        EventBus.getDefault().post(new MsgEvent(MsgEvent.REFRESH_STATUS, uuid));
    }
}
